﻿Public Class frmAddInventory

    ' ***************************************************************
    ' ********************** Private Functions **********************
    ' ***************************************************************

    Private Function GetReadyToAddNewItem() As Boolean

        ' This function will give a new Employee ID Randomly.
        ' Then it get ready to start filling in data. It will
        ' also disable the button so the user cannot try to
        ' save anything without entering the information first.

        Try
            Me.InventoryBindingSource.AddNew()
            ModelIDTextBox.Text = GenerateRandomNumber(100, 999999)
            btnAddInventory.Enabled = False
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message())
            Return False
        End Try
    End Function

    Private Function AddNewItem() As Boolean
        Try
            Me.Validate()
            Me.Atlantic_toyota_atm_dbDataSet.BeginInit()
            Me.InventoryBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.Atlantic_toyota_atm_dbDataSet)
            Me.Atlantic_toyota_atm_dbDataSet.EndInit()
            Me.Atlantic_toyota_atm_dbDataSet.AcceptChanges()
            Me.Atlantic_toyota_atm_dbDataSet.GetChanges()

            Return True
        Catch ex As Exception
            MessageBox.Show("You cannot enter null values.", "Error", _
                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return False
        End Try
    End Function


    ' ***************************************************************
    ' ************************ Private  Subs ************************
    ' ***************************************************************

    Private Sub ModelIDTextBox_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModelIDTextBox.Leave
        ModelIDTextBox.ReadOnly = True
        ModelIDTextBox.BackColor = Color.LightSlateGray
    End Sub

    ' Loads the form and Gets stuff ready for action

    Private Sub frmAddInventory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Try
            ModelIDTextBox.ReadOnly = True

            'Hide the labels for the options
            lblView.Visible = False
            lblDelete.Visible = False

            ' Hides all the main menu labels

            lblHome.Visible = False
            lblVisit.Visible = False
            lblSurvey.Visible = False
            lblStatistics.Visible = False
            'lblInventory.Visible = False
            lblEmployee.Visible = False
            lblClose.Visible = False

            'TODO: This line of code loads data into the 'Atlantic_toyota_atm_dbDataSet.inventory' table. You can move, or remove it, as needed.
            Me.InventoryTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.inventory)
            'TODO: This line of code loads data into the 'Atlantic_toyota_atm_dbDataSet.inventory' table. You can move, or remove it, as needed.
            Me.InventoryTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.inventory)
            'TODO: This line of code loads data into the 'Atlantic_toyota_atm_dbDataSet.inventory' table. You can move, or remove it, as needed.
            Me.InventoryTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.inventory)
            'TODO: This line of code loads data into the 'Atlantic_toyota_atm_dbDataSet.inventory' table. You can move, or remove it, as needed.
            Me.InventoryTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.inventory)
            'TODO: This line of code loads data into the 'Atlantic_toyota_atm_dbDataSet.inventory' table. You can move, or remove it, as needed.
            Me.InventoryTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.inventory)
            'TODO: This line of code loads data into the 'Atlantic_toyota_atm_dbDataSet.inventory' table. You can move, or remove it, as needed.
            Me.InventoryTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.inventory)
            'TODO: This line of code loads data into the 'Atlantic_toyota_atm_dbDataSet.inventory' table. You can move, or remove it, as needed.
            Me.InventoryTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.inventory)

            lblSave.Text = "Add New"
            btnAddInventory.Image = GeneralImageSet.Images("Add")
            btnView.Image = GeneralImageSet.Images("Info")
            btnDelete.Image = GeneralImageSet.Images("Delete")
            btnAdd.Image = GeneralImageSet.Images("AddNew")

            btnAddInventory.Enabled = True
            ModelIDTextBox.Enabled = False
            ModelNameTextBox.Enabled = False


        Catch ex As Exception
            MessageBox.Show("Something weird happened when I tried to load " & _
                            "data from the database.", "Error", _
                             MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try

    End Sub


    Private Sub btnHome_MouseOver(ByVal sender As System.Object, _
               ByVal e As System.EventArgs) Handles btnHome.MouseHover
        lblHome.Visible = True
    End Sub

    Private Sub btnVisit_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnVisit.MouseHover
        lblVisit.Visible = True
    End Sub

    Private Sub btnSurvey_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnSurvey.MouseHover
        lblSurvey.Visible = True
    End Sub

    Private Sub btnStatistics_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnStatistics.MouseHover
        lblStatistics.Visible = True
    End Sub

    'Private Sub btnInventory_MouseOver(ByVal sender As System.Object, _
    '              ByVal e As System.EventArgs) Handles btnInventory.MouseHover
    '    lblInventory.Visible = True
    'End Sub

    Private Sub btnEmployee_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnEmployee.MouseHover
        lblEmployee.Visible = True
    End Sub

    Private Sub btnClose_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnClose.MouseHover
        lblClose.Visible = True
    End Sub


    '**************************************
    ' Hides the labels from the main menu *
    '**************************************


    Private Sub btnHome_MouseLeave(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles btnHome.MouseLeave
        lblHome.Visible = False
    End Sub

    Private Sub btnVisit_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnVisit.MouseLeave
        lblVisit.Visible = False
    End Sub

    Private Sub btnSurvey_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnSurvey.MouseLeave
        lblSurvey.Visible = False
    End Sub

    Private Sub btnStatistics_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnStatistics.MouseLeave
        lblStatistics.Visible = False
    End Sub



    ' Shows label for the view option

    Private Sub btnView_MouseOver(ByVal sender As System.Object, _
                      ByVal e As System.EventArgs) Handles btnView.MouseHover
        lblView.Visible = True
    End Sub

    ' Shows label for the delete option

    Private Sub btnDelete_MouseOver(ByVal sender As System.Object, _
                      ByVal e As System.EventArgs) Handles btnDelete.MouseHover
        lblDelete.Visible = True
    End Sub

    ' Hides label for the view option

    Private Sub btnView_MouseLeave(ByVal sender As System.Object, _
                      ByVal e As System.EventArgs) Handles btnView.MouseLeave
        lblView.Visible = False
    End Sub

    ' Hides label for the delete option

    Private Sub btnDelete_MouseLeave(ByVal sender As System.Object, _
                      ByVal e As System.EventArgs) Handles btnDelete.MouseLeave
        lblDelete.Visible = False
    End Sub




    'Private Sub btnInventory_MouseLeave(ByVal sender As System.Object, _
    '              ByVal e As System.EventArgs) Handles btnInventory.MouseLeave
    '    lblInventory.Visible = False
    'End Sub

    Private Sub btnEmployee_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnEmployee.MouseLeave
        lblEmployee.Visible = False
    End Sub

    Private Sub btnClose_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnClose.MouseLeave
        lblClose.Visible = False
    End Sub



    ' This ensures that the user cannot commit the changes to the
    ' database when there is a null value in the texbox.

    Private Sub ModelIDTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModelIDTextBox.TextChanged
        Try
            If ModelIDTextBox.Text.Length = 0 Then
                btnAddInventory.Enabled = False
            Else
                btnAddInventory.Enabled = True
            End If
        Catch ex As Exception
            MessageBox.Show("Something weird happened when I tried to check " & _
                            "the length of EmployeeIDTextBox.", "Error", _
                             MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub ModelNameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModelNameTextBox.TextChanged
        Try
            If ModelNameTextBox.Text.Length = 0 Then
                btnAddInventory.Enabled = False
            Else
                btnAddInventory.Enabled = True
            End If
        Catch ex As Exception
            MessageBox.Show("Something weird happened when I tried to check " & _
                            "the length of EmployeeNameTextBox.", "Error", _
                             MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub


    ' Switches to form

    Private Sub btnViewInventoryMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        SwitchForm(My.Forms.frmViewInventory)
    End Sub

    Private Sub btnDeleteInventoryMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        SwitchForm(My.Forms.frmDeleteInventory)
    End Sub


    ' Closes all forms

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        CloseAllForms()
    End Sub


    ' Calls GetReadyToAddNewItem or AddNewItem

    Private Sub btnAddInventory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddInventory.Click
        Try
            If lblSave.Text = "Add New" Then

                ' If I am here is because I just came to the form
                ' or because I just saved a value and is starting
                ' up again. So we will enable the text boxes and 
                ' change the button label to Save to make it 
                ' consistent.
                btnAddInventory.Image = GeneralImageSet.Images("Save")
                lblSave.Text = "Save"
                ModelIDTextBox.Enabled = True
                ModelNameTextBox.Enabled = True
                GetReadyToAddNewItem()
                ModelNameTextBox.Focus()

            ElseIf lblSave.Text = "Save" Then

                ' If I am here is because Im about to add a value 
                ' So we will disable the text boxes and change 
                ' the button label to Add New to make it consistent.

                btnAddInventory.Image = GeneralImageSet.Images("Add")
                lblSave.Text = "Add New"
                ModelIDTextBox.Enabled = False
                ModelNameTextBox.Enabled = False
                AddNewItem()

                Me.InventoryTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.inventory)
                Me.InventoryTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.inventory)
                Me.InventoryTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.inventory)

                lblInventory.Focus()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try


    End Sub

    Private Sub EmployeeIDToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmployeeIDToolStripMenuItem.Click
        If ModelIDTextBox.Enabled = True Then
            ModelIDTextBox.ReadOnly = False
            ModelIDTextBox.BackColor = Color.GreenYellow
            ModelIDTextBox.Focus()
            ModelIDTextBox.SelectAll()
        End If

    End Sub

    Private Sub EditToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditToolStripMenuItem.Click
        If ModelIDTextBox.Enabled = False Then
            EmployeeIDToolStripMenuItem.Enabled = False
        Else
            EmployeeIDToolStripMenuItem.Enabled = True
        End If
    End Sub

    Private Sub btnClose_Visit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Dim Confirmation As MsgBoxResult
        Confirmation = MessageBox.Show("Unsaved changes will be lost. Are you sure you want to exit?", _
                                       "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If Confirmation = MsgBoxResult.Yes Then
            CloseAllForms()
        End If
    End Sub

    Private Sub btnHome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHome.Click
        SwitchForm(My.Forms.frmMain)
    End Sub

    Private Sub btnVisit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVisit.Click
        SwitchForm(My.Forms.frmQuickVisit)
    End Sub

    Private Sub btnSurvey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSurvey.Click
        SwitchForm(My.Forms.frmSurvey)
    End Sub

    Private Sub btnStatistics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStatistics.Click
        SwitchForm(My.Forms.frmStatistics)
    End Sub

    Private Sub btnInventory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInventory.Click
        SwitchForm(My.Forms.frmViewInventory)
    End Sub

    Private Sub btnEmployee_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmployee.Click
        SwitchForm(My.Forms.frmViewEmployee)
    End Sub

    Private Sub btnView_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles btnView.Click
        SwitchForm(My.Forms.frmViewInventory)
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        SwitchForm(My.Forms.frmDeleteInventory)
    End Sub
End Class